//
//  HMWelcomeViewController.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/9/2.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        let url = URL(string: HMUserAccountViewModel.sharedModel.account?.profile_image_url ?? "")
        imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default"))
    }

    private func setupUI(){
        self.view.backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        view.addSubview(imageView)
        view.addSubview(messageLabel)
        
        // 添加约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.top.equalTo(200)
            make.centerX.equalTo(self.view)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView)
            make.top.equalTo(imageView.snp_bottom).offset(15)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // 执行约束动画的话，应该要先更新约束
        // 再执行动画 ：layoutIfNeeded
        self.imageView.snp_updateConstraints(closure: { (make) in
            make.top.equalTo(100)
        })
        
        // 阻尼：0-1 如果越小阻尼越小，弹性越大
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.messageLabel.alpha = 1
                }, completion: { (_) in
                    print("动画执行完成")
                    // 切换界面 -- 切换至首页
                    NotificationCenter.default.post(name: NSNotification.Name(HMChangeRootVCNotification), object: nil)
            })
        }
    }
    
// MARK: - 懒加载控件
    private lazy var imageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default"))
        
        // 设置圆角
        imageView.layer.cornerRadius = 45;
        imageView.layer.masksToBounds = true;
        // 设置边线
        imageView.layer.borderColor = UIColor.gray.cgColor
        imageView.layer.borderWidth = 1
        
        return imageView
    }()
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 16)
        label.alpha = 0
        label.text = "欢迎回来"
        return label
    }()

    

}
